#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <netdb.h>
#include <stdio.h>
#include <time.h>
#include <arpa/inet.h>

#define SERVER_TCP_PORT  12347
#define SERV_HOST_ADDR "192.168.1.3"
#define SIZE 512

int err_stop (char* msg)
{
	printf("%s \n",msg);
	exit(-1);
}

int main(int argc, char*argv[ ]) 
{
    	int  sockfd, servlen, n;
    	struct sockaddr_in   cli_addr, serv_addr;
    	char msg[SIZE];
	char name[] = "bonjour";
    	//name = //argv[1];

    /* Fill in the structure "serv_addr" with the address of the
     * server that we want to send to */
	if(argc != 3){
		err_stop ("Pas assez d'arguments pour executer : arret du programme \n");
	}
	else{
    	bzero((char *) &serv_addr, sizeof(serv_addr));
    	serv_addr.sin_family      = AF_INET;
    	serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
    	serv_addr.sin_port        = htons(atoi(argv[2]));

    	/* Open a TCP socket (an Internet datagram socket) */

    	if ( (sockfd = socket(PF_INET,SOCK_STREAM, 0)) < 0)
        	err_stop("client: can't open datagram socket");
	
		/* Connect to the server */

    	if (connect(sockfd , (struct sockaddr *) &serv_addr, sizeof( serv_addr )) < 0){
        	err_stop("connection to server fail ");
        	//exit(-1);
   	}

    	if((send (sockfd,name,strlen(name),0))<0){
		close(sockfd);
		err_stop("send error : Client side");
    	}
		
	
	int compteur = 0;
    	//for(compteur =0;compteur< 60;compteur++){	
		int j;
   		if((j = recv (sockfd, msg,SIZE,0)) > 0){ 
       		//err_stop("client: recv error");
    		msg[j] = '\0';  /* end of string for printf() */
    		printf("%s \n", msg);
		//compteur++;
       }
    close(sockfd);
    }
    exit(0);
}

